package br.usp.ime.ingpos.web.controllers;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;
import br.com.caelum.vraptor.validator.Validations;
import br.usp.ime.ingpos.modelo.TipoPerfil;
import br.usp.ime.ingpos.modelo.Usuario;
import br.usp.ime.ingpos.services.CartaDeRecomendacaoService;
import br.usp.ime.ingpos.services.CurriculoService;
import br.usp.ime.ingpos.services.InscricaoService;
import br.usp.ime.ingpos.services.SenhaService;
import br.usp.ime.ingpos.services.UsuarioService;
import br.usp.ime.ingpos.web.interceptors.Transactional;

@Resource
public class IndexController
{
    private UsuarioSessao usuarioSessao;
    private final Result result;
    private final Validator validador;
    private final SenhaService senhaService;
    private final UsuarioService usuarioService;
    private final InscricaoService inscricaoService;
    private final CurriculoService curriculoService;
    private final CartaDeRecomendacaoService cartaDeRecomendacaoService;

    public IndexController(
        UsuarioSessao usuarioSessao,
        final Result result,
        final Validator validador,
        final SenhaService senhaService,
        final UsuarioService usuarioService,
        final InscricaoService inscricaoService,
        final CurriculoService curriculoService,
        final CartaDeRecomendacaoService cartaDeRecomendacaoService)
    {
        this.usuarioSessao = usuarioSessao;
        this.result = result;
        this.validador = validador;
        this.senhaService = senhaService;
        this.usuarioService = usuarioService;
        this.inscricaoService = inscricaoService;
        this.curriculoService = curriculoService;
        this.cartaDeRecomendacaoService = cartaDeRecomendacaoService;
    }

    @Get
    @Path( "/" )
    public void index()
    {
        if(usuarioSessao.getUsuario().getPerfil().getPerfilID() == 2){
            result.include( "dadosPessoais", usuarioService.dadosPessoaisFinalizados() );
            result.include( "curriculo", curriculoService.curriculoFinalizado() );
            result.include( "cartaDeRecomendacao", cartaDeRecomendacaoService.cartaDeRecomendacaoRecebidas() );
            result.include( "inscricao", inscricaoService.inscricaoFinalizada() );
        }
    }

    @Get
    @Path( "/index/trocaSenha" )
    public void trocaSenha()
    {
    }

    @Post
    @Path( "/index/trocaSenha" )
    @Transactional
    public void trocaSenha(
        final String senhaAtual,
        final String novaSenha,
        final String confirmaNovaSenha )
    {
        final Usuario usuario = usuarioSessao.getUsuario();
        final Long usuarioID = usuarioSessao.getUsuarioId();
        
        validador.checking( new Validations() {
            {
                that( senhaService.isMatchByMD5WithCriptografada( senhaAtual, usuario.getSenha() ), "erro_tipo_senha",
                    "erro_senha_atual" );
                that( novaSenha.equals( confirmaNovaSenha ), "erro_tipo_senha",
                    "erro_senha_sugerida_nao_confere" );
                that( (novaSenha.length()>=6)&& senhaService.atualizarSenha( usuario.getEmail(), novaSenha ),
                    "erro_tipo_senha", "erro_tamanho_da_senha" );

            }
        } );
        validador.onErrorForwardTo( getClass() ).trocaSenha();

        result.include( "messages", "senha_alterado_sucesso" );
        usuarioSessao.setUsuario(usuarioService.procurarPorId( usuarioID ));
        result.redirectTo( getClass() ).index();
    }
}